package com.jcodecraeer.xrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.progressindicator.AVLoadingIndicatorView;

public class LoadingMoreFooter extends LinearLayout {

//    private SimpleViewSwitcher progressCon;
    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;
    public final static int STATE_ERROR = 3;
    public final static int STATE_GONE = 4;

    private TextView mText;
    private String loadingHint;
    private String noMoreHint;
    private String noMoreErrorHint;
    private String loadingDoneHint;
    private ProgressBar progressCon;
    private View view;

    public LoadingMoreFooter(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public LoadingMoreFooter(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

    public void setLoadingHint(String hint) {
        loadingHint = hint;
    }

    public void setNoMoreHint(String hint) {
        noMoreHint = hint;
    }

    public void setLoadingDoneHint(String hint) {
        loadingDoneHint = hint;
    }

//    public void initView(){
//        setGravity(Gravity.CENTER);
//        setLayoutParams(new RecyclerView.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        progressCon = new SimpleViewSwitcher(getContext());
//        progressCon.setLayoutParams(new ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//
//        AVLoadingIndicatorView progressView = new  AVLoadingIndicatorView(this.getContext());
//        progressView.setIndicatorColor(0xffB5B5B5);
//        progressView.setIndicatorId(ProgressStyle.BallSpinFadeLoader);
//        progressCon.setView(progressView);
//
//        addView(progressCon);
//        mText = new TextView(getContext());
//        mText.setText(getContext().getString(R.string.listview_loading));
//
//        if(loadingHint == null || loadingHint.equals("")){
//            loadingHint = (String)getContext().getText(R.string.listview_loading);
//        }
//        if(noMoreHint == null || noMoreHint.equals("")){
//            noMoreHint = (String)getContext().getText(R.string.nomore_loading);
//        }
//        if(loadingDoneHint == null || loadingDoneHint.equals("")){
//            loadingDoneHint = (String)getContext().getText(R.string.loading_done);
//        }
//
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        layoutParams.setMargins( (int)getResources().getDimension(R.dimen.textandiconmargin),0,0,0 );
//
//        mText.setLayoutParams(layoutParams);
//        addView(mText);
//    }

//    public void setProgressStyle(int style) {
//        if(style == ProgressStyle.SysProgress){
//            progressCon.setView(new ProgressBar(getContext(), null, android.R.attr.progressBarStyle));
//        }else{
//            AVLoadingIndicatorView progressView = new  AVLoadingIndicatorView(this.getContext());
//            progressView.setIndicatorColor(0xffB5B5B5);
//            progressView.setIndicatorId(style);
//            progressCon.setView(progressView);
//        }
//    }
public void initView(Context context) {
    view = LayoutInflater.from(context).inflate(R.layout.listview_footer, this);
    mText = findViewById(R.id.listview_foot_more);
//    llFooter = (LinearLayout) findViewById(R.id.ll_footer);
    progressCon = findViewById(R.id.listview_foot_progress);
//    mIvProgress = (ImageView) findViewById(R.id.iv_progress);
//    mAnimationDrawable = (AnimationDrawable) mIvProgress.getDrawable();
//    if (!mAnimationDrawable.isRunning()) {
//        mAnimationDrawable.start();
//    }
    mText.setText(getContext().getString(R.string.listview_loading));

    if (loadingHint == null || loadingHint.equals("")) {
        loadingHint = (String) getContext().getText(R.string.listview_loading);
    }
    if (noMoreHint == null || noMoreHint.equals("")) {
        noMoreHint = (String) getContext().getText(R.string.nomore_loading);
    }
    if (noMoreErrorHint == null || noMoreErrorHint.equals("")) {
        noMoreErrorHint = (String) getContext().getText(R.string.load_more_error);
    }
    if (loadingDoneHint == null || loadingDoneHint.equals("")) {
        loadingDoneHint = (String) getContext().getText(R.string.loading_done);
    }
    setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//    addView(view);
}
    public void  setState(int state) {
        switch(state) {
            case STATE_LOADING:
                progressCon.setVisibility(View.VISIBLE);
                mText.setText(loadingHint);
                this.setVisibility(View.VISIBLE);
                break;
            case STATE_COMPLETE:
                mText.setText(loadingDoneHint);
                progressCon.setVisibility(View.GONE);
                this.setVisibility(View.VISIBLE);
                break;
            case STATE_NOMORE:
//                mText.setText(noMoreHint);
//                progressCon.setVisibility(View.GONE);
                view.setVisibility(View.GONE);
                break;
            case STATE_ERROR:
                mText.setText(noMoreErrorHint);
                progressCon.setVisibility(View.GONE);
                this.setVisibility(View.VISIBLE);
                break;
            case STATE_GONE:
//                mText.setText(noMoreErrorHint);
//                progressCon.setVisibility(View.GONE);
                this.setVisibility(View.GONE);
                break;
        }
    }
}
